package com.fx.smsclient.JsonSerializer;

import cn.hutool.core.util.ObjectUtil;
import com.google.gson.*;
import com.zx.sms.codec.cmpp.msg.CmppDeliverRequestMessage;

import java.lang.reflect.Type;

public class CmppDeliverRequestMessageSerializer implements JsonSerializer<CmppDeliverRequestMessage> {

    static Gson gson = new GsonBuilder().create();

    @Override
    public JsonElement serialize(CmppDeliverRequestMessage src, Type typeOfSrc, JsonSerializationContext context) {
        JsonElement jsonElement = gson.toJsonTree(src);
        JsonObject jsonObject = (JsonObject) jsonElement;
        jsonObject.remove("msgId");
        jsonObject.addProperty("msgId",src.getMsgId().toString());
        jsonObject.remove("header");
        jsonObject.addProperty("commandId",Integer.toUnsignedLong(src.getHeader().getCommandId()));
        jsonObject.addProperty("sequenceId",src.getHeader().getSequenceId());
        if (ObjectUtil.isNotEmpty(jsonObject.getAsJsonObject("reportRequestMessage"))) {
            jsonObject.getAsJsonObject("reportRequestMessage").addProperty("msgId", src.getReportRequestMessage().getMsgId().toString());
        }
        jsonObject.remove("msgContentBytes");
        if  (ObjectUtil.isNotEmpty(src.getMsgContent())) {
            jsonObject.addProperty("msgContent", src.getMsgContent());
        }

        return jsonObject;
    }
}
